﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


// 技能逻辑父类
public abstract class SkillLogicBase
{
    protected Creature _owner;

    protected Creature _curTarget;

    protected TimeLine _timeLine;

    // 本技能逻辑对应的技能对象
    protected CreatureSkill _skillObject;

    // CD管理器
    protected CDMgr _cdMgr;

    // 技能结束时回调
    protected Action _skillEndCallback;

    protected CD _cd = new CD();

    public bool IsInCD { get { return _cdMgr.IsInCD(_cd); } }

    public float LeftCD { get { return _cdMgr.LeftCD(_cd); } }

    public float MaxCD { get { return _cdMgr.MaxCD(_cd); } }

    public void Init(Creature owner, CreatureSkill skillObj, CDMgr cdMgr)
    {
        _owner = owner;
        _skillObject = skillObj;

        _cdMgr = cdMgr;

        _cd.MaxCD = skillObj.Data.CD;

        // 技能开始
        _timeLine = new TimeLine();

        initTimeLine();
    }

    // 子类各自初始化时间线
    protected virtual void initTimeLine()
    {
        Debug.LogError(this.GetType().ToString() + "子类必须提供initTimeLine函数重写");
    }

    // 开始放技能
    public void StartSkill(Creature target, Action skillEndCallback)
    {
        _curTarget = target;
        _skillEndCallback = skillEndCallback;

        _timeLine.Reset();
        _timeLine.Start();

        _cdMgr.Start(_cd);
    }

    // 外界驱动技能
    public void Loop(float deltaTime)
    {
        if (_timeLine != null)
        {
            _timeLine.Loop(deltaTime);
        }
    }

    protected void onSkillStart(int __null)
    {
        _owner.MoveStop();
        if (_curTarget)
        {
            _owner.LookAt2D(_curTarget);
        }
    }

    // 技能结束
    protected void onSkillEnd(int __null)
    {
        _curTarget = null;
        if (_skillEndCallback != null)
        {
            _skillEndCallback();
        }
    }

    protected void onAniEnd(int aniId)
    {
        if (_owner.GetAnimationID() == aniId)
        {
            _owner.SetAnimationID(0);
        }
    }

    // 播动画
    protected void onAni(int animateId)
    {

        _owner.SetAnimationID(animateId);
    }


    protected void onParticle(int particleId)
    {
        var particleData = ParticleTable.Instance[particleId];
        var particleObj = ResMgr.Instance.GetGameObject(particleData.Path);

        particleObj.transform.position = _owner.transform.Find("Slot_Center").position;

        GameObject.Destroy(particleObj, particleData.DuringTime);
    }
}

